package com.starblink.feedback.ui

import android.content.Context
import android.graphics.Rect
import android.view.View
import android.view.inputmethod.InputMethodManager
import com.apollographql.apollo3.api.Optional
import com.starblink.android.basic.ext.addTextChangedListenerClosure
import com.starblink.android.basic.ext.click
import com.starblink.android.basic.ext.toast
import com.starblink.android.basic.network.base.SkGqlClient
import com.starblink.android.basic.user.LoginCheckInterceptor
import com.starblink.android.basic.widget.dialog.BaseBottomSheetFragment
import com.starblink.feedback.databinding.FeedbackDialogLayoutBinding
import com.starblink.rocketreserver.Feedback4ContentAndAppMutation
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.MainScope
import kotlinx.coroutines.launch

class FeedbackPostDialog(val cont: Context, val postId: String) : BaseBottomSheetFragment() {
    lateinit var mBinding: FeedbackDialogLayoutBinding
    override fun getBindingRootView(): View {
        mBinding = FeedbackDialogLayoutBinding.inflate(layoutInflater)
        return mBinding.root
    }

    private var imm: InputMethodManager? = null
    private var mLastDiff = 0
    override fun initView() {
        imm = mContext.getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        mBinding.close.click {
            dismiss()
        }

        mBinding.btnSure.click {
            if (mBinding.input.text?.isNotEmpty() == true) {
                LoginCheckInterceptor.check2Login(cont) {
                    CoroutineScope(Dispatchers.IO).launch {
                        val response = SkGqlClient.skMutation(
                            Feedback4ContentAndAppMutation(
                                Optional.present(postId),
                                mBinding.input.text.toString()
                            ),
                            showDefaultLoading = true
                        )
                        if (response?.data != null && response.data!!.feedback4ContentAndApp != null && response.data!!.feedback4ContentAndApp) {
                            MainScope().launch {
                                toast("Thank you for your advice")
                                dismiss()
                            }
                        }
                    }
                }
            }
        }

        mBinding.flBottomSheet.addOnLayoutChangeListener { view, i, i1, i2, i3, i4, i5, i6, i7 ->
            val r = Rect()
            //获取当前界面可视部分
            dialog.window!!.decorView.getWindowVisibleDisplayFrame(r)
            //获取屏幕的高度
            val screenHeight = dialog.window!!.decorView.rootView.height
            //此处就是用来获取键盘的高度的， 在键盘没有弹出的时候 此高度为0 键盘弹出的时候为一个正数
            val heightDifference = screenHeight - r.bottom
            if (heightDifference <= 0 && mLastDiff > 0) {
                dismiss()
            }
            mLastDiff = heightDifference
        }

        mBinding.flBottomSheet.click {
            imm!!.hideSoftInputFromWindow(mBinding.input.windowToken, 0)
            dismiss()
        }

        mBinding.input.addTextChangedListenerClosure {
            mBinding.tvNum.text = "${it.length}/500"
        }
    }

    override fun dismiss() {
        super.dismiss()
        //dismiss之前重置mLastDiff值避免下次无法打开
        mLastDiff = 0
    }
}